use tauri;
use crate::infra::database;

#[tauri::command]
pub fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    let _ = env_logger::try_init();

    tauri::Builder::default()
        .plugin(tauri_plugin_opener::init())
        .setup(|_app| {
            log::info!("SIP应用启动完成，等待首次连接");
            if let Err(e) = database::init_database() {
                log::error!("数据库初始化失败: {}", e);
            } else {
                log::info!("数据库初始化成功");
            }
            Ok(())
        })
        .invoke_handler(tauri::generate_handler![
            greet,
            crate::controllers::sip_controller::connect_sip,
            crate::controllers::sip_controller::disconnect_sip,
            crate::controllers::sip_controller::get_connection_status,
            crate::controllers::call_controller::make_call,
            crate::controllers::call_controller::hangup_call,
            crate::controllers::call_controller::get_call_history,
            crate::controllers::call_controller::search_call_history,
            crate::controllers::contact_controller::list_contacts,
            crate::controllers::contact_controller::add_contact,
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

